package com.ehomepay.escrow.transition.common.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisClusterConfig {

    @Autowired
    private RedisClusterConfigProperties clusterProperties;

    @Bean
    public RedisConnectionFactory connectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory(
                new RedisClusterConfiguration(clusterProperties.getNodes()));
        /** 如果需要定制连接池,可以使用下面的方式进行配置 */
         JedisPoolConfig pool = new JedisPoolConfig();
         pool.setMaxIdle(clusterProperties.getMaxIdle());
         pool.setMaxTotal(clusterProperties.getMaxTotal());
         pool.setMaxWaitMillis(clusterProperties.getMaxWait());
         factory.setPoolConfig(pool);
        return factory;
    }

    /**
     * 对redis的存取,据需要先行转换成byte[]
     **/
    @Bean
    @Autowired
    public RedisClusterConnection redisClusterConnection(RedisConnectionFactory connectionFactory) {
        return connectionFactory.getClusterConnection();
    }

    /**
     * 该bean可以直接操作String,无需转成byte[]
     **/
    @Bean
    @Autowired
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate(connectionFactory);
        return template;
        
    }

}
